package org.example.server.product.service;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.server.product.bean.CategoryBean;
import org.example.server.product.mapper.CategoryMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    public PageInfo<CategoryBean> list(CategoryBean categoryBean){
        if (categoryBean.getOrder()==null|| categoryBean.getOrder().isEmpty()){
            categoryBean.setOrder("create_time desc");
        }

        PageHelper.startPage(categoryBean.getPageNum(),categoryBean.getPageSize(),categoryBean.getOrder());

        log.debug("分类列表，执行sql语句");
        List<CategoryBean> select = categoryMapper.select(categoryBean);
        return new PageInfo<>(select);
    }

    public void save(CategoryBean categoryBean){
        log.debug("分类保存，检查参数");
        if(categoryBean.getTitle()==null){
            throw new RuntimeException("分类名称不能为空");
        }
        if(categoryBean.getSurface()==null){
            throw new RuntimeException("分类封面不能为空");
        }
        if (categoryBean.getIntro() ==null){
            throw new RuntimeException("分类介绍不能为空");
        }

        log.debug("分类保存，补充参数");
        categoryBean.setId(IdUtil.getSnowflakeNextId());
        categoryBean.setUpdateTime(System.currentTimeMillis());
        categoryBean.setCreateTime(System.currentTimeMillis());

        log.debug("分类保存，执行sql语句");
        int inserted = categoryMapper.insert(categoryBean);
        if(inserted<1){
            throw new RuntimeException("分类保存失败");
        }
    }

    public void delete(CategoryBean categoryBean){
        log.debug("分类删除，检查参数");
        Long id = categoryBean.getId();
        if(id==null){
            throw new RuntimeException("分类id不能为空");
        }
        log.debug("分类删除，执行sql语句");
        int deleted = categoryMapper.delete(categoryBean);
        if(deleted<1){
            throw new RuntimeException("分类删除失败");
        }
    }

    public CategoryBean get(CategoryBean categoryBean){
        log.debug("分类获取，检查参数");
        if (categoryBean.getId()==null){
            throw new RuntimeException("分类id不能为空");
        }
        log.debug("分类获取，执行sql语句");
        return categoryMapper.selectOne(categoryBean);
    }

    public void edit(CategoryBean categoryBean){
        log.debug("分类更新，检查参数");
        if (categoryBean.getId()==null){
            throw new RuntimeException("分类id不能为空");
        }
        if (categoryBean.getTitle()==null){
            throw new RuntimeException("分类名称不能为空");
        }
        if (categoryBean.getIntro()==null){
            throw new RuntimeException("分类介绍不能为空");
        }
        log.debug("分类更新，补充参数");
        categoryBean.setUpdateTime(System.currentTimeMillis());
        log.debug("分类更新，执行sql语句");
        int updated = categoryMapper.update(categoryBean);
        if(updated<1){
            throw new RuntimeException("分类更新失败");
        }
    }
}
